home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / DataOutputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  12.4 KB  |  360 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DataOutputStream.java    1.28 98/04/30
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * A data input stream lets an application write primitive Java data 
  19.  * types to an output stream in a portable way. An application can 
  20.  * then use a data input stream to read the data back in. 
  21.  *
  22.  * @author  unascribed
  23.  * @version 1.28, 04/30/98
  24.  * @see     java.io.DataInputStream
  25.  * @since   JDK1.0
  26.  */
  27. public
  28. class DataOutputStream extends FilterOutputStream implements DataOutput {
  29.     /**
  30.      * The number of bytes written to the data output stream so far. 
  31.      * If this counter overflows, it will be wrapped to Integer.MAX_VALUE.
  32.      */
  33.     protected int written;
  34.  
  35.     /**
  36.      * Creates a new data output stream to write data to the specified 
  37.      * underlying output stream. The counter <code>written</code> is 
  38.      * set to zero.
  39.      *
  40.      * @param   out   the underlying output stream, to be saved for later 
  41.      *                use.
  42.      * @see     java.io.FilterOutputStream#out
  43.      */
  44.     public DataOutputStream(OutputStream out) {
  45.     super(out);
  46.     }
  47.  
  48.     /**
  49.      * Increases the written counter by the specified value
  50.      * until it reaches Integer.MAX_VALUE.
  51.      */
  52.     private void incCount(int value) {
  53.         int temp = written + value;
  54.         if (temp < 0) {
  55.             temp = Integer.MAX_VALUE;
  56.         }
  57.         written = temp;
  58.     }
  59.  
  60.     /**
  61.      * Writes the specified byte (the low eight bits of the argument 
  62.      * <code>b</code>) to the underlying output stream. If no exception 
  63.      * is thrown, the counter <code>written</code> is incremented by 
  64.      * <code>1</code>.
  65.      * <p>
  66.      * Implements the <code>write</code> method of <code>OutputStream</code>.
  67.      *
  68.      * @param      b   the <code>byte</code> to be written.
  69.      * @exception  IOException  if an I/O error occurs.
  70.      * @see        java.io.FilterOutputStream#out
  71.      */
  72.     public synchronized void write(int b) throws IOException {
  73.     out.write(b);
  74.         incCount(1);
  75.     }
  76.  
  77.     /**
  78.      * Writes <code>len</code> bytes from the specified byte array 
  79.      * starting at offset <code>off</code> to the underlying output stream. 
  80.      * If no exception is thrown, the counter <code>written</code> is 
  81.      * incremented by <code>len</code>.
  82.      *
  83.      * @param      b     the data.
  84.      * @param      off   the start offset in the data.
  85.      * @param      len   the number of bytes to write.
  86.      * @exception  IOException  if an I/O error occurs.
  87.      * @see        java.io.FilterOutputStream#out
  88.      */
  89.     public synchronized void write(byte b[], int off, int len)
  90.     throws IOException
  91.     {
  92.     out.write(b, off, len);
  93.     incCount(len);
  94.     }
  95.  
  96.     /**
  97.      * Flushes this data output stream. This forces any buffered output 
  98.      * bytes to be written out to the stream. 
  99.      * <p>
  100.      * The <code>flush</code> method of <code>DataOuputStream</code> 
  101.      * calls the <code>flush</code> method of its underlying output stream.
  102.      *
  103.      * @exception  IOException  if an I/O error occurs.
  104.      * @see        java.io.FilterOutputStream#out
  105.      * @see        java.io.OutputStream#flush()
  106.      */
  107.     public void flush() throws IOException {
  108.     out.flush();
  109.     }
  110.  
  111.     /**
  112.      * Writes a <code>boolean</code> to the underlying output stream as 
  113.      * a 1-byte value. The value <code>true</code> is written out as the 
  114.      * value <code>(byte)1</code>; the value <code>false</code> is 
  115.      * written out as the value <code>(byte)0</code>. If no exception is 
  116.      * thrown, the counter <code>written</code> is incremented by 
  117.      * <code>1</code>.
  118.      *
  119.      * @param      v   a <code>boolean</code> value to be written.
  120.      * @exception  IOException  if an I/O error occurs.
  121.      * @see        java.io.FilterOutputStream#out
  122.      */
  123.     public final void writeBoolean(boolean v) throws IOException {
  124.     out.write(v ? 1 : 0);
  125.     incCount(1);
  126.     }
  127.  
  128.     /**
  129.      * Writes out a <code>byte</code> to the underlying output stream as 
  130.      * a 1-byte value. If no exception is thrown, the counter 
  131.      * <code>written</code> is incremented by <code>1</code>.
  132.      *
  133.      * @param      v   a <code>byte</code> value to be written.
  134.      * @exception  IOException  if an I/O error occurs.
  135.      * @see        java.io.FilterOutputStream#out
  136.      */
  137.     public final void writeByte(int v) throws IOException {
  138.     out.write(v);
  139.         incCount(1);
  140.     }
  141.  
  142.     /**
  143.      * Writes a <code>short</code> to the underlying output stream as two
  144.      * bytes, high byte first. If no exception is thrown, the counter 
  145.      * <code>written</code> is incremented by <code>2</code>.
  146.      *
  147.      * @param      v   a <code>short</code> to be written.
  148.      * @exception  IOException  if an I/O error occurs.
  149.      * @see        java.io.FilterOutputStream#out
  150.      */
  151.     public final void writeShort(int v) throws IOException {
  152.     OutputStream out = this.out;
  153.     out.write((v >>> 8) & 0xFF);
  154.     out.write((v >>> 0) & 0xFF);
  155.     incCount(2);
  156.     }
  157.  
  158.     /**
  159.      * Writes a <code>char</code> to the underlying output stream as a 
  160.      * 2-byte value, high byte first. If no exception is thrown, the 
  161.      * counter <code>written</code> is incremented by <code>2</code>.
  162.      *
  163.      * @param      v   a <code>char</code> value to be written.
  164.      * @exception  IOException  if an I/O error occurs.
  165.      * @see        java.io.FilterOutputStream#out
  166.      */
  167.     public final void writeChar(int v) throws IOException {
  168.     OutputStream out = this.out;
  169.     out.write((v >>> 8) & 0xFF);
  170.     out.write((v >>> 0) & 0xFF);
  171.     incCount(2);
  172.     }
  173.  
  174.     /**
  175.      * Writes an <code>int</code> to the underlying output stream as four
  176.      * bytes, high byte first. If no exception is thrown, the counter 
  177.      * <code>written</code> is incremented by <code>4</code>.
  178.      *
  179.      * @param      v   an <code>int</code> to be written.
  180.      * @exception  IOException  if an I/O error occurs.
  181.      * @see        java.io.FilterOutputStream#out
  182.      */
  183.     public final void writeInt(int v) throws IOException {
  184.     OutputStream out = this.out;
  185.     out.write((v >>> 24) & 0xFF);
  186.     out.write((v >>> 16) & 0xFF);
  187.     out.write((v >>>  8) & 0xFF);
  188.     out.write((v >>>  0) & 0xFF);
  189.     incCount(4);
  190.     }
  191.  
  192.     /**
  193.      * Writes a <code>long</code> to the underlying output stream as eight
  194.      * bytes, high byte first. In no exception is thrown, the counter 
  195.      * <code>written</code> is incremented by <code>8</code>.
  196.      *
  197.      * @param      v   a <code>long</code> to be written.
  198.      * @exception  IOException  if an I/O error occurs.
  199.      * @see        java.io.FilterOutputStream#out
  200.      */
  201.     public final void writeLong(long v) throws IOException {
  202.     OutputStream out = this.out;
  203.     out.write((int)(v >>> 56) & 0xFF);
  204.     out.write((int)(v >>> 48) & 0xFF);
  205.     out.write((int)(v >>> 40) & 0xFF);
  206.     out.write((int)(v >>> 32) & 0xFF);
  207.     out.write((int)(v >>> 24) & 0xFF);
  208.     out.write((int)(v >>> 16) & 0xFF);
  209.     out.write((int)(v >>>  8) & 0xFF);
  210.     out.write((int)(v >>>  0) & 0xFF);
  211.     incCount(8);
  212.     }
  213.  
  214.     /**
  215.      * Converts the float argument to an <code>int</code> using the 
  216.      * <code>floatToIntBits</code> method in class <code>Float</code>, 
  217.      * and then writes that <code>int</code> value to the underlying 
  218.      * output stream as a 4-byte quantity, high byte first. If no 
  219.      * exception is thrown, the counter <code>written</code> is 
  220.      * incremented by <code>4</code>.
  221.      *
  222.      * @param      v   a <code>float</code> value to be written.
  223.      * @exception  IOException  if an I/O error occurs.
  224.      * @see        java.io.FilterOutputStream#out
  225.      * @see        java.lang.Float#floatToIntBits(float)
  226.      */
  227.     public final void writeFloat(float v) throws IOException {
  228.     writeInt(Float.floatToIntBits(v));
  229.     }
  230.  
  231.     /**
  232.      * Converts the double argument to a <code>long</code> using the 
  233.      * <code>doubleToLongBits</code> method in class <code>Double</code>, 
  234.      * and then writes that <code>long</code> value to the underlying 
  235.      * output stream as an 8-byte quantity, high byte first. If no 
  236.      * exception is thrown, the counter <code>written</code> is 
  237.      * incremented by <code>8</code>.
  238.      *
  239.      * @param      v   a <code>double</code> value to be written.
  240.      * @exception  IOException  if an I/O error occurs.
  241.      * @see        java.io.FilterOutputStream#out
  242.      * @see        java.lang.Double#doubleToLongBits(double)
  243.      */
  244.     public final void writeDouble(double v) throws IOException {
  245.     writeLong(Double.doubleToLongBits(v));
  246.     }
  247.  
  248.     /**
  249.      * Writes out the string to the underlying output stream as a 
  250.      * sequence of bytes. Each character in the string is written out, in 
  251.      * sequence, by discarding its high eight bits. If no exception is 
  252.      * thrown, the counter <code>written</code> is incremented by the 
  253.      * length of <code>s</code>.
  254.      *
  255.      * @param      s   a string of bytes to be written.
  256.      * @exception  IOException  if an I/O error occurs.
  257.      * @see        java.io.FilterOutputStream#out
  258.      */
  259.     public final void writeBytes(String s) throws IOException {
  260.     OutputStream out = this.out;
  261.     int len = s.length();
  262.     for (int i = 0 ; i < len ; i++) {
  263.         out.write((byte)s.charAt(i));
  264.     }
  265.     incCount(len);
  266.     }
  267.  
  268.     /**
  269.      * Writes a string to the underlying output stream as a sequence of 
  270.      * characters. Each character is written to the data output stream as 
  271.      * if by the <code>writeChar</code> method. If no exception is 
  272.      * thrown, the counter <code>written</code> is incremented by twice 
  273.      * the length of <code>s</code>.
  274.      *
  275.      * @param      s   a <code>String</code> value to be written.
  276.      * @exception  IOException  if an I/O error occurs.
  277.      * @see        java.io.DataOutputStream#writeChar(int)
  278.      * @see        java.io.FilterOutputStream#out
  279.      */
  280.     public final void writeChars(String s) throws IOException {
  281.     OutputStream out = this.out;
  282.     int len = s.length();
  283.     for (int i = 0 ; i < len ; i++) {
  284.         int v = s.charAt(i);
  285.         out.write((v >>> 8) & 0xFF);
  286.         out.write((v >>> 0) & 0xFF);
  287.     }
  288.     incCount(len * 2);
  289.     }
  290.  
  291.     /**
  292.      * Writes a string to the underlying output stream using UTF-8 
  293.      * encoding in a machine-independent manner. 
  294.      * <p>
  295.      * First, two bytes are written to the output stream as if by the 
  296.      * <code>writeShort</code> method giving the number of bytes to 
  297.      * follow. This value is the number of bytes actually written out, 
  298.      * not the length of the string. Following the length, each character 
  299.      * of the string is output, in sequence, using the UTF-8 encoding 
  300.      * for the character. If no exception is thrown, the counter 
  301.      * <code>written</code> is incremented by the total number of 
  302.      * bytes written to the output stream. This will be at least two 
  303.      * plus the length of <code>str</code>, and at most two plus 
  304.      * thrice the length of <code>str</code>.
  305.      *
  306.      * @param      str   a string to be written.
  307.      * @exception  IOException  if an I/O error occurs.
  308.      */
  309.     public final void writeUTF(String str) throws IOException {
  310.     OutputStream out = this.out;
  311.     int strlen = str.length();
  312.     int utflen = 0;
  313.  
  314.     for (int i = 0 ; i < strlen ; i++) {
  315.         int c = str.charAt(i);
  316.         if ((c >= 0x0001) && (c <= 0x007F)) {
  317.         utflen++;
  318.         } else if (c > 0x07FF) {
  319.         utflen += 3;
  320.         } else {
  321.         utflen += 2;
  322.         }
  323.     }
  324.  
  325.     if (utflen > 65535)
  326.         throw new UTFDataFormatException();          
  327.  
  328.     out.write((utflen >>> 8) & 0xFF);
  329.     out.write((utflen >>> 0) & 0xFF);
  330.     for (int i = 0 ; i < strlen ; i++) {
  331.         int c = str.charAt(i);
  332.         if ((c >= 0x0001) && (c <= 0x007F)) {
  333.         out.write(c);
  334.         } else if (c > 0x07FF) {
  335.         out.write(0xE0 | ((c >> 12) & 0x0F));
  336.         out.write(0x80 | ((c >>  6) & 0x3F));
  337.         out.write(0x80 | ((c >>  0) & 0x3F));
  338.         incCount(2);
  339.         } else {
  340.         out.write(0xC0 | ((c >>  6) & 0x1F));
  341.         out.write(0x80 | ((c >>  0) & 0x3F));
  342.         incCount(1);
  343.         }
  344.     }
  345.     incCount(strlen + 2);
  346.     }
  347.  
  348.     /**
  349.      * Returns the current value of the counter <code>written</code>, 
  350.      * the number of bytes written to this data output stream so far.
  351.      * If the counter overflows, it will be wrapped to Integer.MAX_VALUE.
  352.      *
  353.      * @return  the value of the <code>written</code> field.
  354.      * @see     java.io.DataOutputStream#written
  355.      */
  356.     public final int size() {
  357.     return written;
  358.     }
  359. }
  360.